import { IAnswerChecker } from "./IAnswerChecker";
import { IAnswer } from "@/model/Answer";
import { ICheckResponse } from "@/model/CheckResponse";
import { IQuestion } from "@/model/Question";
import { ValidatorResponse } from "@/Types";
import { FixedLengthValidator } from "@/controlor/Validator/FixedLengthValidator";
import { IValidator } from "@/controlor/Validator/IValidator";
import { RegularExpressionValidator } from "@/controlor/Validator/RegularExpressionValidator";
import { NoDuplicateValidator } from "@/controlor/Validator/NoDuplicateValidator";

export class GuessNumAnswerChecker implements IAnswerChecker {
  Check(question: IQuestion, answer: IAnswer): ICheckResponse {
    let numA = 0;
    let numB = 0;

    for (let i = 0; i < 4; i++) {
      const indexOf = question.Question.indexOf(answer.Answer[i]);
      if (indexOf !== -1 && indexOf === i) {
        numA += 1;
      } else if (indexOf !== -1) {
        numB += 1;
      }
    }

    return {
      Message: numA + "A" + numB + "B",
      IsRight: numA === 4
    };
  }

  Validate(answer: IAnswer): ValidatorResponse[] {
    const rules: IValidator<string>[] = [
      FixedLengthValidator(4),
      RegularExpressionValidator(`^[0-9]+$`, "输入必须都是数字"),
      NoDuplicateValidator
    ];
    return rules.map(validator => {
      return validator(answer.Answer);
    });
  }
}
